package serializable;

import java.io.*;

/**
 * @author jukg
 * @date 2024/09/10 9:48
 * @description
 */
public class SerializableTest {


    public static void main(String[] args) throws IOException, ClassNotFoundException {
//定义myObj对象
MyObject myObj = new MyObject();
myObj.name="hello world";
//创建一个包含对象进行反序列化信息的”object”数据文件
FileOutputStream fos = new FileOutputStream("object");
ObjectOutputStream os=new ObjectOutputStream(fos);
//writeObject()方法将myObj对象写入object文件
os.writeObject(myObj);
os.close();
//从文件中反序列化obj对象
FileInputStream fis = new FileInputStream("object");
ObjectInputStream ois=new ObjectInputStream(fis);
//恢复对象
MyObject objectFromDisk=(MyObject)ois.readObject();
System.out.println(objectFromDisk.name);
ois.close();
    }
}


class MyObject{


    public String name;

    private void readObject(){

    }
}
